﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using ZhimeiSoft.Framework.Common.Entity;
using ZhimeiSoft.Framework.Server.Base;
using ZhimeiSoft.Framework.Server.Connector.DAL.Interface;

namespace ZhimeiSoft.Framework.Server.Connector.DAL
{
    public class UserDal : BaseDal, IUserDal
    {
        #region IUserDal 成员

        public DataSet GetAllUsers()
        {
            string strSQL = UserDal_Const.SQL_SELECT_GETALLUSERS;
            DbCommand cmdSelect = DB.GetSqlStringCommand(strSQL);
            return DB.ExecuteDataSet(cmdSelect);
        }

        public void InsertUser(UserEntity user)
        {
            string sql = UserDal_Const.SQL_INSERT_INSERTUSER;
            using (IDbConnection conn = DB.CreateConnection())
            {
                conn.Open();
                IDbTransaction trans = conn.BeginTransaction();
                try
                {
                    DbCommand cmd = DB.GetSqlStringCommand(sql);
                    int userid = this.GetMaxID("TUsers", "UserID");
                    user.UserId = userid + 1;
                    DB.AddInParameter(cmd, "UserID", DbType.String, user.UserId);
                    DB.AddInParameter(cmd, "UserName", DbType.String, user.UserName);
                    DB.AddInParameter(cmd, "Password", DbType.String, user.Password);
                    DB.AddInParameter(cmd, "DisplayName", DbType.String, user.DisplayName);
                    DB.ExecuteNonQuery(cmd, trans as DbTransaction);
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }
        }

        public void UpdateUser(UserEntity user)
        {
            string sql = UserDal_Const.SQL_UPDATE_UPDATEUSER;
            using (IDbConnection conn = DB.CreateConnection())
            {
                conn.Open();
                IDbTransaction trans = conn.BeginTransaction();
                try
                {
                    DbCommand cmd = DB.GetSqlStringCommand(sql);
                    DB.AddInParameter(cmd, "UserID", DbType.String, user.UserId);
                    DB.AddInParameter(cmd, "UserName", DbType.String, user.UserName);
                    DB.AddInParameter(cmd, "Password", DbType.String, user.Password);
                    DB.AddInParameter(cmd, "DisplayName", DbType.String, user.DisplayName);
                    DB.ExecuteNonQuery(cmd, trans as DbTransaction);
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }
        }

        public void DeleteUser(int id)
        {
            string sql = UserDal_Const.SQL_DELETE_DELETEUSER;
            using (IDbConnection conn = DB.CreateConnection())
            {
                conn.Open();
                IDbTransaction trans = conn.BeginTransaction();
                try
                {
                    DbCommand cmd = DB.GetSqlStringCommand(sql);
                    DB.AddInParameter(cmd, "UserID", DbType.String, id);
                    DB.ExecuteNonQuery(cmd, trans as DbTransaction);
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }
        }

        #endregion
    }
}
